<?php
	// +----------------------------------------------------------------------
	// | 地址验证器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------
	namespace app\event\store;

	use app\common\event\Base;
	use app\model\ucenter\User as UserModel;
	use app\model\store\Activity as ActivityModel;
	use think\Exception;
	use think\facade\Lang;
	use yicmf\tools\HttpService;

	class Activity extends Base
	{


		/**
		 * 活动列表
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function lists($param, $user = null)
		{
			try {


				$data['message'] = '操作成功';
				$data['data'] = ActivityModel::where('status', 1)->paginate();
				$data['code'] = 0;
			} catch (\Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}


		/**
		 * 活动详情
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function detail($param, $user = null)
		{
			try {


				$data['message'] = '操作成功';
				$data['data'] = ActivityModel::where('status', 1)->where('id', $param['id'])->find();
				$data['code'] = 0;
			} catch (\Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 通用更新方法
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function update($param, $user = null)
		{
			try {

				if (!empty($param['id'])) {
					// 修改
					$activity = ActivityModel::where('id', $param['id'])->find();
					if (!$activity) {
						throw new Exception('活动不存在');
					}
					unset($param['id']);
					if (!$activity->save($param)) {
						throw new Exception('地址未更新');
					}
				} else {
					if ($user->addresses()->where('status', 1)->count() > 19) {
						throw new Exception('地址最多添加20个');
					}
					$activity = ActivityModel::create($param);
					if (!$activity) {
						throw new Exception('新增活动失败');
					}
				}
				$data['message'] = '操作成功';
				$data['data'] = $activity;
				$data['code'] = 0;
			} catch (\Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 通用更新方法
		 * @param array $param
		 * @param \app\model\store\Store $store
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function join($param, $store = null)
		{
			try {
				// 修改
				$activity = ActivityModel::where('id', $param['id'])->find();
				if (!$activity) {
					throw new Exception('活动不存在');
				}
				$store->goods()->where('status', 1)->update(['activity_id' => null]);
				// 加入方式
				if (1 == $param['join_type']) {
					// 按分类
					if (empty($param['store_category_id'])) {
						throw new Exception('请选择参加的分类');
					}
					$store_category_id = $store->categories()->where('id','in', $param['store_category_id'])->where('status', 1)->find();
					if (!$store_category_id) {
						throw new Exception('分类不合法');
					}
					$store->goods()->where('status', 1)->where('store_category_id','in', $param['store_category_id'])->update(['activity_id' => $activity['id']]);
				} elseif (2 == $param['join_type']) {
					if (empty($param['good_ids'])) {
						throw new Exception('请选择参加的商品');
					}
					$store->goods()->where('status', 1)->where('id', 'in', $param['good_ids'])->update(['activity_id' => $activity['id']]);
				}else{
					// 全部
					$store->goods()->where('status', 1)->update(['activity_id' => $activity['id']]);
				}

				$data['message'] = '操作成功';
				$data['data'] = $activity;
				$data['code'] = 0;
			} catch (\Exception $e) {
				$data['code'] = 1;
				$data['line'] = $e->getLine();
				$data['file'] = $e->getFile();
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

	}
